package com.bsoft.gol.hcb.service.templateSheet.processor.transProcessor.impl;

import com.bsoft.gol.hcb.service.templateSheet.entity.TemplateSheet;
import com.bsoft.gol.hcb.service.templateSheet.localTransService.LocalTransService;
import com.bsoft.gol.hcb.service.templateSheet.localTransService.impl.McPreFilingService;
import com.bsoft.gol.hcb.service.templateSheet.processor.transProcessor.TransProcessor;
import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("localTransProcessor")
public class LocalTransProcessor implements TransProcessor, InitializingBean {

    @Autowired
    McPreFilingService mcPreFilingService;

    private Map<String, LocalTransService> localTransServiceMap;

    @Override
    public void afterPropertiesSet() throws Exception {
        localTransServiceMap = new HashMap<>();
        localTransServiceMap.put("35", mcPreFilingService);
    }

    @Override
    public TemplateSheet inTrans(TemplateSheet templateSheet) throws Exception {
        LocalTransService localTransService = getLocalTransService(templateSheet.getTemplateId().toString());
        Map<String, Object> sheetParam = localTransService.getById(templateSheet.getRecordId());
        templateSheet.setSheetParam(sheetParam);
        return templateSheet;
    }

    @Override
    public TemplateSheet outTrans(TemplateSheet templateSheet) throws Exception {
        LocalTransService localTransService = getLocalTransService(templateSheet.getTemplateId().toString());
        String recordId = localTransService.save(templateSheet.getSheetParam());
        templateSheet.setRecordId(recordId);
        return templateSheet;
    }

    private LocalTransService getLocalTransService(String templateId) throws Exception {
        if (localTransServiceMap == null) {
            throw new Exception("未找到本地存储服务");
        }

        LocalTransService localTransService = localTransServiceMap.get(templateId);
        if (localTransService == null) {
            throw new Exception("未找到本地存储服务");
        }
        return localTransService;
    }

    public Map<String, LocalTransService> getLocalTransServiceMap() {
        return localTransServiceMap;
    }

    public void setLocalTransServiceMap(Map<String, LocalTransService> localTransServiceMap) {
        this.localTransServiceMap = localTransServiceMap;
    }


}
